package com.chinasofti.framework.base;

import java.io.*;

import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.entity.*;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;

import android.graphics.*;
import android.os.*;

/**
 * Asynchronous HTTP connections
 * 
 * @author Greg Zavitz & Joseph Roth
 */
public class HttpConnection implements Runnable {

	public static final int DID_START = 0;
	public static final int DID_ERROR = 1;
	public static final int DID_SUCCEED = 2;
	
	public enum Status {
		START,
		ERROR,
		SUCEEED
	}

	public enum Method {
		GET,
		POST,
		PUT,
		DELETE,
		BITMAP
	}

	private String url;
	private Method method;
	private Handler handler;
	private String data;

	private HttpClient httpClient;

	public HttpConnection() {
		this(new Handler());
	}

	public HttpConnection(Handler _handler) {
		handler = _handler;
	}

	public void create(Method method, String url, String data) {
		this.method = method;
		this.url = url;
		this.data = data;
	}

	public void get(String url) {
		create(Method.GET, url, null);
	}

	public void post(String url, String data) {
		create(Method.POST, url, data);
	}

	public void put(String url, String data) {
		create(Method.PUT, url, data);
	}

	public void delete(String url) {
		create(Method.DELETE, url, null);
	}

	public void bitmap(String url) {
		create(Method.BITMAP, url, null);
	}

	public void run() {
		handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START));
		httpClient = new DefaultHttpClient();
		HttpConnectionParams.setSoTimeout(httpClient.getParams(), 25000);
		try {
			HttpResponse response = null;
			switch (method) {
			case GET:
				response = httpClient.execute(new HttpGet(url));
				break;
				
			case POST:
				HttpPost httpPost = new HttpPost(url);
				httpPost.setEntity(new StringEntity(data));
				response = httpClient.execute(httpPost);
				break;
				
			case PUT:
				HttpPut httpPut = new HttpPut(url);
				httpPut.setEntity(new StringEntity(data));
				response = httpClient.execute(httpPut);
				break;
				
			case DELETE:
				response = httpClient.execute(new HttpDelete(url));
				break;
				
			case BITMAP:
				response = httpClient.execute(new HttpGet(url));
				processBitmapEntity(response.getEntity());
				break;
			}
			
			if (method != Method.BITMAP) {
				processEntity(response.getEntity());
			}
			
		} 
		catch (Exception e) {
			handler.sendMessage(Message.obtain(handler,
					HttpConnection.Status.ERROR.ordinal(), e));
		}
	}

	private void processEntity(HttpEntity entity) 
			throws IllegalStateException, IOException {
		BufferedReader br = new BufferedReader(
				new InputStreamReader(entity.getContent()));
		String line, result = "";
		while ((line = br.readLine()) != null) {
			result += line;
		}	
		Message message = Message.obtain(handler, Status.SUCEEED.ordinal(), result);
		handler.sendMessage(message);
	}

	private void processBitmapEntity(HttpEntity entity) throws IOException {
		BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
		Bitmap bm = BitmapFactory.decodeStream(bufHttpEntity.getContent());
		handler.sendMessage(Message.obtain(handler, Status.SUCEEED.ordinal(), bm));
	}

}
